﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NP.Model;

namespace NP.BL
{
    public class EmployeesSummary
    {
        public IEnumerable<EmployeeSummaryViewModel> GetEmployee()
        {
            using (var db = new NorthwindDataClassesDataContext())
            {
                //var employees = context.Employees.Select(n => n.Employees);
                //return (IEnumerable<Employee>)employees;

                //var employees = from o in db.Orders
                //                join od in db.Order_Details on o.OrderID equals od.OrderID
                //                join e in db.Employees on o.EmployeeID equals e.EmployeeID
                //                group o by o.EmployeeID into g
                //                select new { Id = (int)g.Key, FN = g.Select(d => d.Employee.FirstName).ToString(), LN = g.Select(d => d.Employee.LastName).ToString(), TotalSold = g.Sum(n => n.Order_Details.Sum(m => m.Quantity * m.UnitPrice * (decimal)m.Discount)), CityList = g.Select(u => u.ShipCity).ToArray() };

                //foreach (var a in employees)
                //{
                //    Console.WriteLine(a.FN+ ' ' + a.LN);
                //}
                //Console.ReadLine();

                var employees = db.Employees.ToArray()
                   .Select(n =>
                       new EmployeeSummaryViewModel
                       {
                           FN = n.FirstName,
                           LN = n.LastName,
                           Id = n.EmployeeID,
                           TotalSold = n.Orders
                           .Select(m => m.Order_Details
                           .Sum(k => k.Quantity * k.UnitPrice * (decimal)(1- k.Discount))).Sum()
                       }).ToArray();

                //foreach (var a in employess)
                //{
                //    Console.WriteLine(a.Name + ' ' + a.Sum);
                //}
                //Console.ReadLine();

                //var empterritories = db.Employees.ToArray()
                //    .Select(n => 
                //        new
                //        {
                //            Name = n.LastName,
                //            City = n.EmployeeTerritories
                //            .SelectMany(m => m.TerritoryID)
                //        });

                var empteritories = db.EmployeeTerritories.ToArray()
                    .Select(n => new
                    {
                        Name = n.Employee.LastName,
                        City = n.Territory.TerritoryDescription
                    }).Select(n => new { Name = n.Name, City = n.City }).GroupBy(a => a.Name);

                foreach (var item in employees)
                {
                    var x = empteritories.Where(n => n.Key == item.LN).SelectMany(n => n.Select(m=>m.City.Trim())).ToArray();
                    item.CityList = new string[x.Length];
                    item.CityList = x;
                }
                
                //foreach (var a in empteritories)
                //{
                //    Console.Write(a.Key + ' ');
                //    foreach (var i in a)
                //    {
                //        Console.Write(i.City.Trim() + ',');
                //    }
                //    Console.WriteLine();
                //}
                //Console.ReadLine();

                return employees;
            }
        }

        //public IEnumerable<EmployeeSummaryViewModel> GetReport()
        //{
        //    //var employees = GetEmployee();
        //    // employees.Select(n=>n.Orders.Select(m=>m.Order_Details.Select(k=>k.
        //    //

        //    //return (IEnumerable<EmployeeSummaryViewModel>)employees;//logika
        //}

    }

}

